package com.hnevc.androidui9;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView tvCity;
    TextView tvTemp;
    Button btnSh,btnBj,btnGz;
    List<WeatherInfo> weatherInfos;
    int index=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
        try {
            initData();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        WeatherInfo weatherInfo = weatherInfos.get(index);
        tvCity.setText(weatherInfo.getName());
        tvTemp.setText(weatherInfo.getTemp());
    }
    //初始化数据获取xml中的信息，保存到weatherinfos列表中
    private void initData() throws IOException, XmlPullParserException {
        InputStream is = getResources().openRawResource(R.raw.weather1);
        try {
            weatherInfos=WeatherService.getInfosFromXML(is);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private  void initEvent(){
        btnGz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index = 2;
                WeatherInfo weatherInfo = weatherInfos.get(index);
                tvCity.setText(weatherInfo.getName());
                tvTemp.setText(weatherInfo.getTemp());
                btnBj.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        index = 1;
                        WeatherInfo weatherInfo = weatherInfos.get(index);
                        tvCity.setText(weatherInfo.getName());
                        tvTemp.setText(weatherInfo.getTemp());
                        btnSh.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                index = 0 ;
                                WeatherInfo weatherInfo = weatherInfos.get(index);
                                tvCity.setText(weatherInfo.getName());
                                tvTemp.setText(weatherInfo.getTemp());
                            }
                        });
                    }
                });

            }
        });
    }

    private void initView() {

        tvCity=findViewById(R.id.tv_city);
        tvTemp=findViewById(R.id.tv_temp);
        btnBj=findViewById(R.id.btn_bj);
        btnGz=findViewById(R.id.btn_gz);
        btnSh=findViewById(R.id.btn_sh);
    }
}

